package com.wang.service.impl;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wang.dao.AtAdmins;
import com.wang.dao.AtAdminsQuery;
import com.wang.service.AtAdminsService;
import com.wang.mapper.AtAdminsMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
* @author wjq
*/
@Service
public class AtAdminsServiceImpl extends ServiceImpl<AtAdminsMapper, AtAdmins>
    implements AtAdminsService{
    /**
      * @param query 查询对象
        *  @return
             */
    @Override
    public List<AtAdmins> findList(IPage<AtAdmins> page, AtAdminsQuery query) {
        return this.baseMapper.findList(page, query);
    }

    @Override
    public int deleteBatch(AtAdminsQuery query) {
        return this.baseMapper.deleteBatch(query);
    }

    @Override
    public int updateBatch(AtAdminsQuery query) {
        return this.baseMapper.updateBatch(query);
    }

    @Override
    public AtAdmins findOne(AtAdminsQuery query) {
        return this.baseMapper.findOne(query);
    }

    /**
     * 对数据额外处理(非用户填写的数据)
     * @param entity
     */
    private void proccedUpdateData(AtAdmins entity){
        if (Objects.isNull(entity.getCreateTime())) {
            entity.setCreateTime(new Date());
        }
        entity.setUpdateTime(new Date());
        /* 判断对象是否为空 */
        if (Objects.isNull(entity.getPubTime())) {
            entity.setPubTime(new Date());
        }
        /* 处理邮件,邮箱不能重复 */
        AtAdminsQuery paramQuery = new AtAdminsQuery();
        AtAdmins paramEntity = paramQuery.getEntity();
        paramEntity.setEmail(entity.getEmail());
        AtAdmins resultDb = this.findOne(paramQuery);
        /* 什么要判断邮箱重复;
         * 邮箱必须查询到,并且id和当前的id不一样
         * emailExistsFlag:false:表示邮箱存在,请更换邮箱(验证不通过;
         * true:验证通过
         *
         * 保存的时候如何验证:只要查询出邮箱,直接不让保存
         * 更新的时候: 邮箱得存在,并且数据库中查询的id和当前对象的id得不一样才行
         *  */
        boolean emailExistsFlag = true;
        if (Objects.isNull(entity.getId())) {
            /* 说明是保存 */
            if (Objects.nonNull(resultDb)) {
                emailExistsFlag = false;
            }
        } else {
            /* 更新 */
            emailExistsFlag = resultDb.getId().equalsIgnoreCase(entity.getId());
        }
        /* 邮箱重复,不能再往下进行,需要抛出异常 */
        Assert.isTrue(emailExistsFlag, "邮箱重复,请更换邮箱");
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean save(AtAdmins entity) {
        this.proccedUpdateData(entity);
        /* 新增加的记录默认未删除 */
        return super.save(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateById(AtAdmins entity) {
        this.proccedUpdateData(entity);
        /* 把数据从缓存中重新Redis */
        return super.updateById(entity);
    }
}




